// pages/server/server.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    str: '1.接单三小时内处理',
    list: [],
    des: "",
    promise: "",
    service: [],
    areaNum: [],
    serverName: "",
    isLocal: false,
    isLocalArea: false,
    refuseReason:"",
    isCheck:0
  },
  // 删除地区
  cancelArea: function(e) {
    var index = e.currentTarget.dataset.index;
    this.data.list.splice(index, 1);
    this.setData({
      list: this.data.list
    })
  },
  // 删除服务内容
  cancelSer: function(e) {
    var that = this;
    var serviceName = [];
    var index = e.currentTarget.dataset.index;
    this.data.service.splice(index, 1);
    this.setData({
      service: this.data.service
    })
   
    for (var i = 0; i < this.data.service.length; i++) {
      if (that.data.service[i].id==undefined){
        serviceName.push(that.data.service[i])
      }else{
        serviceName.push(that.data.service[i].id);
      }
      
    }
    console.log(serviceName);
    this.setData({
      serviceName: serviceName
    })
    console.log(this.data.serviceName);
  },
  // 详情描述
  des: function(e) {
    this.setData({
      des: e.detail.value,
    })
  },
  // 服务承诺
  promise: function(e) {
    this.setData({
      promise: e.detail.value
    })
  },
  /**
   * 提交事件
   */
  submit: function() {
    var that = this;
    console.log(this.data.des);
    console.log(this.data.promise);
    console.log(this.data.serviceName);
    var list = that.data.list.join(",");
    if (this.data.des == "" || this.data.des == null) {
      wx.showToast({
        title: '请填写描述',
        duration: 2000,
        icon: "none"
      })
    } else if (this.data.promise == "" || this.data.promise == null) {
      wx.showToast({
        title: '请填写承诺',
        duration: 2000,
        icon: "none"
      })
    } else {
      console.log(that.data.des);
      console.log(that.data.promise);
      console.log(that.data.service)
      if (that.data.serviceName==undefined){
        that.setData({
          serviceName:that.data.service
        });
      }
      wx.showModal({
        title: "提示",
        content: '是否确认提交',
        confirmText: "确认",
        cancelText: "取消",
        success: function(res) {
          if (res.confirm) {
            wx.request({
              url: 'https://www.xiaozheng8.com/auth/content',
              method: "post",
              data: {
                userId: wx.getStorageSync("userId"),
                des: that.data.des,
                promise: that.data.promise,
                introduceList: that.data.serviceName,
                area: list
              },
              success: function(res) {
                console.log(res)
              }
            })
            wx.showModal({
              title: '提示',
              content: '信息提交成功',
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  var pages = getCurrentPages();
                  var currPage = pages[pages.length - 1]; //当前页面
                  var prevPage = pages[pages.length - 2]; //上一个页面

                  prevPage.setData({
                      skill: 1
                    }),
                    wx.navigateBack({

                    })
                }
              }
            })
          }
        }

      })
    }

  },
  // 添加地区
  address: function() {
    wx.navigateTo({
      url: '../area/area',
    })
  },
  addService: function() {
    wx.navigateTo({
      url: '../addservice/addservice',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this;
    wx.request({
      url: 'https://www.xiaozheng8.com/auth/contentFind',
      method: "post",
      data: {
        userId: wx.getStorageSync("userId"),
      },
      success: function (res) {
        console.log(res);
        that.setData({
          promise: res.data.data.promise,
          des: res.data.data.des,
          service: res.data.data.introduceList,
          list: res.data.data.area,
          isCheck:res.data.data.isCheck,
          refuseReason:res.data.data.refuseReason
        })
        var list = that.data.list.split(",");
        console.log(list);
        that.setData({
          list: list,
        })
        if (that.data.isCheck == 9) {
          wx.showModal({
            title: '拒绝原因',
            content: that.data.refuseReason,
            showCancel:false
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    console.log(that.data.isLocal);
    console.log(that.data.isLocalArea);
    var serviceName=[];
    if (that.data.isLocal == true) {
      var data = wx.getStorageSync("area");
      var area = [];
      // var serviceName = [];
      var serviceList = [];
      for (var i = 0; i < data.length; i++) {
        if (data[i].click == true) {
          area.push(data[i].area)
          that.setData({
            list: area,
            // isLocal: true
          })
          console.log(that.data.list);


        }
      };
    } else {

    }
    if (that.data.isLocalArea == true) {
      var service = wx.getStorageSync("serviceList");
      console.log(service);
      var serviceList=[];
      for (var i = 0; i < service.length; i++) {
        if (service[i].click == true) {
          console.log(service[i]);
          var a = service[i]
          serviceList.push(a)
          that.setData({
            service: serviceList
          })
          // console.log(that.data.server)

        }

      }
      for (var i = 0; i < this.data.service.length; i++) {
        // console.log(that.data.service);
        var a = that.data.service[i].name
        serviceName.push(a);
        console.log(serviceName);
      }
      this.setData({
        serviceName: serviceName
  
      })

      console.log(that.data.service);


    } else {

    }

    // for (var i = 0; i < this.data.service.length; i++) {
    //   that.data.serverNum.push(that.data.service[i].id)
    // }
    // console.log(that.data.serverNum);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})